package cn.zb.controller;

import cn.zb.api.flow.dto.FlowDTO;
import cn.zb.api.flow.vo.DbVO;
import cn.zb.common.annotation.DictQuery;
import cn.zb.common.util.RedisUtil;
import cn.zb.common.util.Result;
import cn.zb.entity.auth.SysUser;
import cn.zb.mapper.FlowMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import org.dromara.warm.flow.core.dto.FlowParams;
import org.dromara.warm.flow.core.entity.Definition;
import org.dromara.warm.flow.core.entity.Instance;
import org.dromara.warm.flow.core.enums.SkipType;
import org.dromara.warm.flow.core.service.DefService;
import org.dromara.warm.flow.core.service.InsService;
import org.dromara.warm.flow.orm.entity.FlowDefinition;
import org.dromara.warm.flow.orm.entity.FlowInstance;
import org.dromara.warm.flow.orm.entity.FlowUser;
import org.dromara.warm.flow.orm.mapper.FlowDefinitionMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("flow")
@Log
@Api("流程管理")
public class FlowController {
    @Resource
    InsService insService;
    @Resource
    DefService defService;
    @Resource
    FlowDefinitionMapper definitionMapper;

    @PostMapping("list")
    @ApiOperation("查询所有流程图部署")
    public Result<IPage<FlowDefinition>> list(@RequestBody FlowDTO dto){
        // 条件构造
        QueryWrapper<FlowDefinition> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(dto.getFlowcode()),"flow_code",dto.getFlowcode());
        qw.like(StringUtils.isNotBlank(dto.getFlowname()),"flow_name",dto.getFlowcode());
        qw.eq(dto.getFb()!=null,"is_publish",dto.getFb());
        IPage<FlowDefinition> page = new Page<>(dto.getPage(),dto.getSize());
        definitionMapper.selectPage(page,qw);
        return Result.data(page);
    }

    @Resource
    FlowMapper flowMapper;


    @RequestMapping("db")
    @ApiOperation("我的待办")
    public Result<List<DbVO>> db(@RequestHeader("token") String token){
        SysUser user = (SysUser) redisUtil.get("login:"+token);
        List<String> list = new ArrayList<>();
        // 角色
        user.getRoles().forEach(r->{
            if(!"员工".equals(r)){
                list.add("role:"+r);
            }
        });
        list.add("user:"+user.getUserId());
        list.add("dept:"+user.getDeptId());
        List<DbVO> dbVOS = flowMapper.daiBan(list);
        // 遍历 设置流程定义数据
        dbVOS.forEach(vo->{
            Definition def = defService.getById(vo.getDefinitionId());
            vo.setTitle(def.getFlowName());
            vo.setType(def.getExt());
        });
        return Result.data(dbVOS);
    }

    @RequestMapping("lz")
    @ApiOperation("任务流转")
    public Result lz(Long id,@RequestHeader("token") String token){
        SysUser user = (SysUser) redisUtil.get("login:"+token);
        List<String> list = new ArrayList<>();
        // 给与角色权限
        user.getRoles().forEach(r->{list.add("role:"+r);});
        // 给与用户权限
        list.add("user:"+user.getUserId());
        // 给与部门权限
        list.add("dept:"+user.getDeptId());

        FlowParams params = FlowParams.build()
                .skipType(SkipType.PASS.getKey())
                .permissionFlag(list);
        insService.skipByInsId(id,params);
        return Result.ok("办理成功");
    }

    // 根据实例id查询任务流程图
    @RequestMapping("rwlct")
    public String rwlct(Long id) throws IOException {
        String s = defService.flowChart(id);
        return "data:image/png;base64,"+s;
    }
    @Resource
    RedisUtil redisUtil;
    @RequestMapping("test")
    @PreAuthorize("hasRole('超级管理员')")
    public String s(@RequestHeader("token") String token){
        log.info("测试日志");
        SysUser user = (SysUser) redisUtil.get("login:"+token);
        System.out.println(user);
        return "测试日志";
    }

    @RequestMapping("test2")
    @DictQuery
    public Result a(){
        return Result.data(new FlowUser());
    }

    @RequestMapping("test3")
    @DictQuery
    public Result aa(){
        return Result.data(new ArrayList<>());
    }
}

